package com.hmdp.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.*;
import java.time.format.DateTimeFormatter;

@Component
public class RedisIdWorker {
    //时间戳
    private static final Long BEGIN_TIME=1738948248L;
    //序列号的位数
    private static final int COUNT_BITS=32;
    @Autowired
    private RedisTemplate redisTemplate;

    public long nextId(String keyPrefix)
    {
        LocalDateTime now = LocalDateTime.now();
        long nowSecond=now.toEpochSecond(ZoneOffset.UTC);
        long timestamp=nowSecond-BEGIN_TIME;
        String day= now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        long count = redisTemplate.opsForValue().increment("icr:"+keyPrefix+":"+day);
        return timestamp<<COUNT_BITS|count;
    }
}
